package com.lbw.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lbw.config.Comment;
import com.lbw.config.Md5;
import com.lbw.entity.ManagerEntity;
import com.lbw.entity.StudentEntity;
import com.lbw.entity.SubjectEntity;
import com.lbw.entity.TeacherEntity;
import com.lbw.result.Result;
import com.lbw.result.ResultFactory;
import com.lbw.service.ManagerEntityService;
import com.lbw.service.StudentEntityService;
import com.lbw.service.SubjectEntityService;
import com.lbw.service.TeacherEntityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.UUID;

/**
 * @author lbw
 * @date 2020/9/15 22:52
 */
@RestController
@Api(tags = "用户登录注册")
@RequestMapping("/userInfo")
public class LoginController {

    @Autowired
    private StudentEntityService studentEntityService;

    @Autowired
    private TeacherEntityService teacherEntityService;

    @Autowired
    private ManagerEntityService managerEntityService;

    @Autowired
    private SubjectEntityService subjectEntityService;
    /**
     * 用户登录
     * @return
     */
    @PostMapping("login")
    @ApiOperation("用户登录")
        public Result loginUser(@ApiParam("用户名") @RequestParam(value = "name", required = true) String name,
                            @ApiParam("用户密码") @RequestParam(value = "password", required = true) String password,
                            @ApiParam("用户标识") @RequestParam(value = "flag", required = true) Integer flag) {
        //进行md5加秘
        StringBuilder md5Password = Md5.md5Encryption(password + name);

        if (0 == flag) {
            //为学生
            LambdaQueryWrapper<StudentEntity> lambdaStuWrapper = new LambdaQueryWrapper<>();
            lambdaStuWrapper.eq(StudentEntity::getName,name)
                    .eq(StudentEntity::getPassword,md5Password.toString());
            StudentEntity studentEntityOne = studentEntityService.getOne(lambdaStuWrapper);
            if (studentEntityOne == null) {
                return ResultFactory.fail("用户名或密码错误，请重试！");
            }
            return ResultFactory.success(studentEntityOne);
        } else if (1 == flag) {
            //为教师
            LambdaQueryWrapper<TeacherEntity> lambdaTeaWrapper = new LambdaQueryWrapper<>();
            lambdaTeaWrapper.eq(TeacherEntity::getName,name)
                    .eq(TeacherEntity::getPassword,md5Password);
            TeacherEntity teacherEntityOne = teacherEntityService.getOne(lambdaTeaWrapper);
            if (teacherEntityOne == null) {
                return ResultFactory.fail("用户名或密码错误，请重试！");
            }
            return ResultFactory.success(teacherEntityOne);
        } else {
            //为管理员
            LambdaQueryWrapper<ManagerEntity> lambdaManWrapper = new LambdaQueryWrapper<>();
            lambdaManWrapper.eq(ManagerEntity::getName,name)
                    .eq(ManagerEntity::getPassword,md5Password);
            ManagerEntity managerEntityOne = managerEntityService.getOne(lambdaManWrapper);
            if (managerEntityOne == null) {
                return ResultFactory.fail("用户名或密码错误，请重试！");
            }
            return ResultFactory.success(managerEntityOne);
        }

    }

    /**
     * 用户注册
     * @return
     */
    @PostMapping("register")
    @ApiOperation("用户注册")
    public Result registerUser(@ApiParam("用户名") @RequestParam(value = "name", required = true) String name,
                            @ApiParam("用户密码") @RequestParam(value = "password", required = true) String password,
                            @ApiParam("用户标识") @RequestParam(value = "flag", required = true) Integer flag) {
        //进行md5加秘
        StringBuilder md5Password = Md5.md5Encryption(password + name);

        if (0 == flag) {
            //为学生
            String stuId = UUID.randomUUID().toString().replaceAll("-", "");
            StudentEntity studentEntity = new StudentEntity();
            studentEntity.setName(name);
            studentEntity.setPassword(md5Password.toString());
            studentEntity.setType(flag);
            studentEntity.setId(stuId);
            boolean save = studentEntityService.save(studentEntity);

            if (!save) {
                return ResultFactory.fail("注册失败");
            }
            //注册学生后往科目表中存入该学生的科目
            //插入行测
            SubjectEntity subjectEntityXc = new SubjectEntity();
            subjectEntityXc.setSubjectName("行政职业能力测验");
            subjectEntityXc.setSubjectPic(Comment.SUBJECT_XC);
            subjectEntityXc.setType(1);
            subjectEntityXc.setStuId(stuId);
            //插入申论
            SubjectEntity subjectEntitySl = new SubjectEntity();
            subjectEntitySl.setSubjectName("申论");
            subjectEntitySl.setSubjectPic(Comment.SUBJECT_SL);
            subjectEntitySl.setStuId(stuId);
            subjectEntitySl.setType(2);
            //批量插入
            LinkedList<SubjectEntity> entityLinkedList = new LinkedList<>();
            entityLinkedList.add(subjectEntityXc);
            entityLinkedList.add(subjectEntitySl);
            boolean saveBatch = subjectEntityService.saveBatch(entityLinkedList);
            if (!saveBatch) {
                return ResultFactory.fail("注册失败");
            }
            return ResultFactory.success(true);
        } else if (1 == flag) {
            //为教师
            TeacherEntity teacherEntity = new TeacherEntity();
            teacherEntity.setName(name);
            teacherEntity.setPassword(md5Password.toString());
            teacherEntity.setType(flag);
            boolean save = teacherEntityService.save(teacherEntity);
            if (!save) {
                return ResultFactory.fail("注册失败");
            }
            return ResultFactory.success(true);
        } else {
            //为管理员
            ManagerEntity managerEntity = new ManagerEntity();
            managerEntity.setName(name);
            managerEntity.setPassword(md5Password.toString());
            managerEntity.setType(flag);
            boolean save = managerEntityService.save(managerEntity);
            if (!save) {
                return ResultFactory.fail("注册失败");
            }
            return ResultFactory.success(true);
        }
    }

    /**
     * 退出登录
     * @param userId
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("退出")
    public Result logout(@ApiParam("用户Id号") @RequestParam(value = "userId",required = true) String userId,
                         @ApiParam("用户标识") @RequestParam(value = "flag", required = true) Integer flag) {
        if (0 == flag) {
            //为学生
            StudentEntity studentEntity = studentEntityService.getById(userId);
            if (studentEntity == null) {
                return ResultFactory.fail("退出失败！");
            }
            return ResultFactory.success(true);
        } else if (1 == flag) {
            //为教师

            TeacherEntity teacherEntity = teacherEntityService.getById(userId);
            if (teacherEntity == null) {
                return ResultFactory.fail("退出失败！");
            }
            return ResultFactory.success(true);
        } else {
            //为管理员
            ManagerEntity managerEntity = managerEntityService.getById(userId);
            if (managerEntity == null) {
                return ResultFactory.fail("退出失败！");
            }
            return ResultFactory.success(true);
        }
    }


}
